package mylab.common.utils;

import cn.hutool.core.text.CharSequenceUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import mylab.base.constants.Oauth2Const;
import mylab.common.context.UserContext;
import mylab.common.exceptions.TokenException;
import mylab.common.model.LoginUser;
import mylab.utils.common.NumberUtil;


/**
 * token安全检测工具类
 *
 * @author pangu
 */
@Slf4j
public class SecurityUtil {


    /**
     * 从HttpServletRequest里获取token
     *
     * @param request HttpServletRequest
     * @return token
     */
    public static String getHeaderToken(HttpServletRequest request) {
        return request.getHeader(Oauth2Const.HEADER_TOKEN);
    }

    /**
     * 从HttpServletRequest里获取token
     *
     * @param request HttpServletRequest
     * @return token
     */
    public static String getToken(HttpServletRequest request) {
        String headerToken = getHeaderToken(request);
        if (CharSequenceUtil.isBlank(headerToken)) {
            throw new TokenException("没有携带Token信息！");
        }
        return CharSequenceUtil.isNotBlank(headerToken) ? TokenUtil.getToken(headerToken) : "";
    }

    /**
     * 从Token解析获取Claims对象
     *
     * @param token Mate-Auth获取的token
     * @return Claims
     */
    public static Claims getClaims(String token) {
        Claims claims = null;
        if (CharSequenceUtil.isNotBlank(token)) {
            try {
                claims = TokenUtil.getClaims(token);
            } catch (Exception e) {
                throw new TokenException("Token已过期！");
            }
        }
        return claims;
    }

    /**
     * 从HttpServletRequest获取LoginUser信息
     *
     * @param request HttpServletRequest
     * @return LoginUser
     */
    public static LoginUser getUsername(HttpServletRequest request) {

        String token = getToken(request);
        Claims claims = getClaims(token);
        assert claims != null;


        // 然后根据token获取用户登录信息，这里省略获取用户信息的过程
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(String.valueOf(claims.get(Oauth2Const.MATE_USER_ID)));
        loginUser.setAccount((String) claims.get(Oauth2Const.MATE_USER_NAME));
        loginUser.setRoleId(String.valueOf(claims.get(Oauth2Const.MATE_ROLE_ID)));
        loginUser.setTenantId(String.valueOf(claims.get(Oauth2Const.MATE_TENANT_ID)));
        loginUser.setType(NumberUtil.stoi(String.valueOf(claims.get(Oauth2Const.MATE_TYPE))));
        UserContext.setUser(loginUser);
        return loginUser;
    }
}
